﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace CompanyRatings.PerfTest.Models.Query
{
    [XmlRoot("companyRatingsQuery")]
    public class CrQuery
    {
        [XmlElement("group")]
        public CrQueryGroup Group { get; set; }

        [XmlElement("weightMatrix")]
        public CrQueryWeightMatrix WeightMatrix { get; set; }

        [XmlElement("sectorCategories")]
        public CrQuerySectorCategories SectorCategories { get; set; }

        [XmlElement("companies")]
        public CrQueryCompanies Companies { get; set; }

        [XmlElement("portfolios")]
        public CrQueryPortfolios Portfolios { get; set; }

        [XmlElement("universes")]
        public CrQueryUniverses Universes { get; set; }


        /// <summary>
        /// Ctor.
        /// </summary>
        public CrQuery()
        {
        }


        public string ToXml()
        {
            XmlSerializer serializer = new XmlSerializer(typeof(CrQuery));
            StringBuilder xmlStrBuilder = new StringBuilder();

            using (StringWriter sw = new StringWriter(xmlStrBuilder))
            {
                serializer.Serialize(sw, this);
            }

            return xmlStrBuilder.ToString();
        }

        public static CrQuery FromXml(string xml)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(CrQuery));

            using (StringReader sr = new StringReader(xml))
            {
                CrQuery instance = (CrQuery)serializer.Deserialize(sr);
                return instance;
            }
        }
    }
}
